package com.climb.moive.controller;

import com.climb.moive.entity.Admin;
import com.climb.moive.framework.mvc.BaseController;
import com.climb.moive.service.*;
import com.climb.moive.service.impl.*;
import com.climb.moive.util.CommmonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author Administrator
 * 管理员表现层
 * 管理员登陆注销
 */
@WebServlet("/adminIndex")
public class AdminIndexController extends BaseController {


    public String doLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return "/view/admin/AdminLogin.jsp";

    }

    //登录
    public String login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1、接收参数   BeanUtils
        Admin admin = new Admin();
        CommmonUtils.parseBean(admin, request.getParameterMap());

        //2、调用业务
        AdminService adminService = new AdminServiceImpl();
        Admin result = adminService.login(admin);

        //3、返回结果
        if (result != null) {
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);

            return "redirect:" + request.getContextPath() + "/adminIndex?method=index";
        } else {
            request.setAttribute("message", "用户名/密码出错，请重新登录");
            return "forward:/adminIndex?method=doLogin";
        }
    }

    /*登录成功*/
    public String adminIndex(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        return "/view/admin/AdminLogin.jsp";
    }

    /*注销*/
    public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();

        //在首页中有登录的链接
        return "redirect:" + request.getContextPath() + "/adminIndex";
    }

    //管理员首页
    @Override
    public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查出的统计数据，然后在界面中显示
        HttpSession session = request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");
        if (admin != null) {
            MemberService memberService = new MemberServiceImpl();

            MovieEvalService movieEvaluationService = new MoiveEvalServiceImpl();
            MovieReviewService movieReviewService = new MovieReviewServiceImpl();
            MovieService movieService=new MovieServiceImpl() ;
            request.setAttribute("memberNum", memberService.countMember());
            request.setAttribute("movieNum", movieService.countMovieNum());
            request.setAttribute("movieEvaluationNum", movieEvaluationService.countMovieEvaluationNum());
            request.setAttribute("movieReviewNum", movieReviewService.countMovieReviewNum());
            return "/view/admin/AdminIndex.jsp";
        } else {
            return "/view/admin/AdminLogin.jsp";
        }

    }

}
